package cn.xd.base.application.service.common.impl;

import cn.xd.base.application.assembler.DictAssembler;
import cn.xd.base.application.assembler.DictGroupAssembler;
import cn.xd.base.application.service.common.DictAppService;
import cn.xd.base.basic.constants.CacheConstants;
import cn.xd.base.basic.type.DictCode;
import cn.xd.base.basic.type.DictGroupId;
import cn.xd.base.domain.bean.command.AddDictCommand;
import cn.xd.base.domain.bean.command.AddDictGroupCommand;
import cn.xd.base.domain.bean.command.UpdateDictCommand;
import cn.xd.base.domain.bean.command.UpdateDictGroupCommand;
import cn.xd.base.domain.bean.response.DictDTO;
import cn.xd.base.domain.bean.response.DictGroupDTO;
import cn.xd.base.domain.entity.Dict;
import cn.xd.base.domain.entity.DictGroup;
import cn.xd.base.domain.repository.DictGroupRepository;
import cn.xd.base.domain.repository.DictRepository;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author : mirzaLin
 * @data : 2023/11/20
 * @description : 字典
 **/
@Service
public class DictAppServiceImpl implements DictAppService {

    @Resource
    private DictRepository dictRepository;
    @Resource
    private DictGroupRepository dictGroupRepository;

    /**
     * 修改字典
     *
     * @return 修改后的字典
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = CacheConstants.DICT_CACHE, key = "#command.getOldCode()+'*'")
    public DictDTO updateDict(UpdateDictCommand command, Long userId) {
        // 获取原先的字典
        Dict dict = dictRepository.find(DictCode.of(command.getOldCode())).isEmptyThrow("字典不存在");
        // 执行修改操作
        dict.update(command, userId);
        dictRepository.save(dict);
        return DictAssembler.INSTANCE.toDto(dict);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DictGroupDTO updateDictGroup(UpdateDictGroupCommand command, Long userId) {
        // 获取原先的字典分组
        DictGroup dictGroup = dictGroupRepository.find(DictGroupId.of(command.getId())).isEmptyThrow("字典分组不存在");
        // 执行修改操作
        dictGroup.update(command, userId);
        dictGroupRepository.save(dictGroup);
        return DictGroupAssembler.INSTANCE.toDto(dictGroup);
    }

    /**
     * 新增字典
     *
     * @return 新增的字典
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public DictDTO addDict(AddDictCommand command, Long userId) {
        // 执行增加操作
        Dict add = Dict.add(command, userId);
        dictRepository.save(add);
        return DictAssembler.INSTANCE.toDto(add);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = CacheConstants.DICT_CACHE, key = "#dictCode+'*'")
    public void deleteDict(String dictCode) {
        Dict dict = dictRepository.find(DictCode.of(dictCode)).isEmptyThrow("不存在该字典");
        dictRepository.remove(dict);
    }

    /**
     * 新增字典
     *
     * @return 新增的字典
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public DictGroupDTO addDictGroup(AddDictGroupCommand command, Long userId) {
        // 执行增加操作
        DictGroup add = DictGroup.add(command, userId);
        dictGroupRepository.save(add);
        return DictGroupAssembler.INSTANCE.toDto(add);
    }
}
